import django_filters
from django_filters import FilterSet
from django.db.models import Q
from order.models import Order


class OrderFilter(FilterSet):
    # 设置过滤字段及条件，默认为相等匹配
    totalmax = django_filters.NumberFilter(field_name='total', lookup_expr='gte')
    totalmin = django_filters.NumberFilter(field_name='total', lookup_expr='lte')

    movie_id = django_filters.NumberFilter(field_name='movie_id')

    # 自定义过滤，条件不符合执行另一个
    def movie_id_filter(self, queryset, name, value):
        result = queryset.filter(Q(movie_id=value) | Q(user__order__movie_id=value))
        return result

    class Meta:
        model = Order
        fields = ['total', ]
